package com.lagou.stage6module1.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {
    /**
     * 正常队列
     */
    @Bean
    public Queue queue(){
        Map<String,Object> props = new HashMap<>();
        props.put("x-message-ttl",10000);
        props.put("x-dead-letter-exchange", "ex.order.dlx");
        props.put("x-dead-letter-routing-key", "order.dlx");
        Queue queue = new Queue("order.queue", true, false, false, props);
        return queue;
    }

    @Bean
    public Exchange exchange(){
        DirectExchange exchange = new DirectExchange("ex.order", true, false, null);
        return exchange;
    }

    @Bean
    public Binding binding(){
        return BindingBuilder.bind(queue()).to(exchange()).with("order").noargs();
    }

    /**
     * 死信队列
     */
    @Bean
    public Queue queueDlx(){
        Queue queue = new Queue("order.queue.dlx", true, false, false);
        return queue;
    }

    @Bean
    public Exchange exchangeDlx(){
        DirectExchange exchange = new DirectExchange("ex.order.dlx", true, false, null);
        return exchange;
    }

    @Bean
    public Binding bindingDlx(){
        return BindingBuilder.bind(queueDlx()).to(exchangeDlx()).with("order.dlx").noargs();
    }
}
